/***
 * @Author:xxx
 * @Date:2020-08-10 12:12:31
 * @LastModifiedBy:xxx
 * @Last Modified time:2020-08-12 14:54:04
 */

import React , { useEffect } from 'react';
import './admin.css';
import { Layout } from 'antd';
import { HashRouter,Switch,Route,Redirect,useHistory } from 'react-router-dom';
import AdminHeader from '../../components/header/adminHeader';
import { getToken } from '../../utils/cookies';
import { DndProvider } from 'react-dnd';
import Backend from 'react-dnd-html5-backend'; 
import {useDispatch, useSelector} from 'react-redux';
import AdminMenus from '../../components/menu/menu'
import { setChinaMap} from '../../redux/actions';


import Home from '../home/home';
import UserMgt from '../user-mgt/user-mgt';
import OriganizeMgt from '../organize-mgt/origanize-mgt';
import WorkshopAdmin from '../workshop-mgt/workshop-admin';
import RolesMgt from '../roles-mgt/roles-mgt';
import DataMgt from '../data-mgt/data-mgt';
import DataProcess from '../data-process/data-process';  
import DataShow from '../data-show';
import ChinaMap from '../map';


const {  Content } = Layout;

const Admin =()=> {
  
    const user=useSelector(state=>state.user)
    const hasChinaMap=useSelector(state=>state.hasChinaMap)
    const history = useHistory()
    const dispatch = useDispatch()

    useEffect(()=>{
      if(localStorage.getItem('chinaMap')){
        if(hasChinaMap.chinaMap=='/'){
          dispatch(setChinaMap({chinaMap:localStorage.getItem('chinaMap')}))
        }
      }
    },[JSON.stringify(hasChinaMap)])

    
    

    if(!getToken()) {
      return <Redirect to='/login' /> 
    }
  
    
    return (
      <Layout>
        <AdminHeader />
        <Content className="site-layout" style={{ marginTop: 100, Height: '100%'}}>
          <DndProvider backend={Backend}>
            <div className="site-layout-background" style={{ Height: '100%' }}>
            <AdminMenus/>
              <HashRouter>
                <Switch>
                  <Route exact path='/home' component={Home} />
                  <Route exact path='/user-mgt' component={UserMgt} />
                  <Route exact path='/orgzanition-mgt' component={OriganizeMgt} />   {/* /origanize-mgt */}
                  <Route path='/workshop-mgt' component={WorkshopAdmin} />
                  <Route exact path='/roles-mgt' component={RolesMgt} />
                  <Route path='/data-mgt' component={DataMgt} />
                  <Route path='/data-process' component={DataProcess} />
                  <Route path='/data-show' component={DataShow} />
                  <Route path='/chinaMap' component={ChinaMap} />
                  {/* <Redirect from='/' to='/login' /> */}
                  <Redirect from='/' to={`${hasChinaMap.chinaMap}`} />
                </Switch>
              </HashRouter>
            </div>
          </DndProvider>
        </Content>
      </Layout>      
    )
  
}

export default Admin;
